package com.qd.panda.service.card.third.sync;

import com.cdqidi.db.BaseService;
import com.cdqidi.exception.ApiException;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.common.panda.domain.entity.card.third.sync.ThirdPartySyncDTO;
import com.qd.panda.mapper.card.ThirdPartySyncMapper;
import com.qd.panda.model.ThirdPartyList;
import com.qd.panda.model.ThirdPartySync;
import com.qd.panda.service.card.third.ThirdPartyService;
import com.qd.panda.service.third.coupon.CouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * 第三方劵同步
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ThirdPartySyncHolderService extends BaseService<ThirdPartySyncMapper, ThirdPartySync> {
    private final ThirdPartySyncService thirdPartySyncService;
    private final ThirdPartyListService thirdPartyListService;
    private final CouponService couponService;
    private final ThirdPartyService thirdPartyService;

    /**
     * 保存
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @Transactional(rollbackFor = Exception.class)
    public ThirdPartySyncDTO save(ThirdPartySyncDTO dto) {
        if(Boolean.FALSE.equals(thirdPartyService.isExistsById(dto.getThirdParty()))){
            throw new ApiException("第三方券商值错误");
        }
        List<String> list = couponService.getCoupon(dto.getEvent());
        if (null == list || list.isEmpty()) {
            throw new ApiException("到远程获取券失败");
        }
        List<ThirdPartyList> thirdPartyLists = new ArrayList<>();
        for (String coupon : list) {
            ThirdPartyList thirdPartyList = new ThirdPartyList();
            thirdPartyList.setThirdCouponCode(coupon);
            thirdPartyList.setThirdParty(dto.getThirdParty());
            thirdPartyList.setEvent(dto.getEvent());
            thirdPartyList.setState(PandaConstant.USE_STATE_NEG_1);
            thirdPartyLists.add(thirdPartyList);
        }
        thirdPartyListService.saveBatch(thirdPartyLists);
        dto.setTotalQuantity((long) list.size());
        dto.setUseQuantity(0L);
        ThirdPartySyncDTO save = thirdPartySyncService.save(dto);
        thirdPartyLists.clear();
        list.clear();
        return save;
    }

    /**
     * 批量删除
     *
     * @param ids 主键集合
     */
    public void removeBatchIds(List<String> ids) {
        for (String event : ids) {
            List<ThirdPartyList> thirdPartyLists = thirdPartyListService.getListByEvent(event);
            if (thirdPartyLists.stream().anyMatch(p -> !p.getState().equals(PandaConstant.USE_STATE_NEG_1))) {
                throw new ApiException("不能删除");
            }
            List<String> deleteIds = thirdPartyLists.stream().map(ThirdPartyList::getThirdCouponCode).collect(toList());
            thirdPartyListService.removeBatchIds(deleteIds);
            thirdPartySyncService.removeById(event);
        }
    }
}